<?php

// Prevent much happening if this script is called directly
if ( !defined('INCS') ) exit;

class HTML_Element
{
    function HTML_Element(){}

    var $tagname;
    var $attributes;
    var $innerValue;

    // Performs no sanitisation - assumes this has already been done!

    public function getString()
    {
        if ( !is_string( $this->tagname ) || empty( $this->tagname ) ) return '';

        $s  = '';
        $s .= "<{$this->tagname}";

        if ( is_string( $this->attributes ) && !empty( $this->attributes ) )
        {
            $s .= " {$this->attributes}";
        }

        if ( $this->requires_close_tag() )
        {
            $s .= '>';
            if ( is_string( $this->innerValue ) && !empty( $this->innerValue ) )
            {
                if ( $this->is_blocklevel() ) $s .= "\n";
                $s .= $this->innerValue;
            }
            $s .= "</{$this->tagname}>";
        }
        else
        {
           $s .= ' />';
        }

        $s .= "\n";

        return $s;
    }

    private function requires_close_tag()
    {
        $tags_self_close = array( 'input' => 1, 'br' => 1);

        return !array_key_exists($this->tagname, $tags_self_close);
    }

    private function is_blocklevel()
    {
        $tags_blocklevel = array( 'div' => 1, 'select' => 1);

        return array_key_exists($this->tagname, $tags_blocklevel);
    }


}